<?php

namespace app\admin\controller\contents;
use app\common\controller\Backend;

/**
 * 员工管理
 *
 * @icon fa fa-users
 */
class yuangong extends Backend
{

    /**
     * @var \app\admin\model\UserGroup
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Yuangong');
        $this->view->assign('sexList',$this->model->sexList());
        $this->view->assign('tpList',$this->model->tpList());
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            //print_r($where);

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => &$v) {
                $v['tp'] = $this->model->tpList($v['tp']);
                $v['sex'] = $this->model->sexList($v['sex']);
                $v['atime'] =date("Y-m-d H:i:s",$v['atime']);
                $position='/erweima/erweima_'.$v['id'].'.png';
                $erweima=ROOT_PATH.'public'.$position;
                if(!is_file($erweima)){
                    $this->create_erweima($v['id']);
                }
                $v['erweima']=$position;
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    public function info($ids = NULL){
        $row = $this->model->get($ids);
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }
    public function erweima($ids = NULL){
        $row = $this->model->get($ids);
        if(empty($row)){
            $this->error('员工不存在');
        }
        $this->create_erweima($row['id']);
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }
    private function create_erweima($id){
        import('phpqrcode.index',EXTEND_PATH);
        $PhpQRCode = new \PhpQRCode();
        $PhpQRCode->set('pngTempDir',ROOT_PATH.'public/erweima/');
        $PhpQRCode->set('date','test');
        $PhpQRCode->set('pngTempName', 'erweima_'.$id .'.png');
        $PhpQRCode->init();
    }
    public function pingjia($ids = NULL){
        $where['ispinglun']=1;
        $where['yg_id']=$ids;
        $list=Model('orders')->where($where)->order('id desc')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $info=Model('orders_pingjia')->where(array('order_id'=>$v['id']))->find();
                $yg_info=Model('kehu')->where(array('id'=>$v['yg_id']))->find();
                if(!empty($info)){
                    $list[$k]['atime']=date('Y-m-d H:i:s',$info['atime']);
                    $list[$k]['taidu']=taidu($info['taidu']);
                    $list[$k]['kehu_nickname']=$yg_info['nickname'];
                    $list[$k]['zhiliang']=zhiliang($info['zhiliang']);
                    $list[$k]['info']=$info['info'];
                }
            }
        }else{
            $list=array();
        }
        $this->view->assign('list',$list);
        return $this->view->fetch();
    }
    public function kehu($ids = NULL){
        $where['yg_id']=$ids;
        $list=Model('kehu')->where($where)->select();
        $list=empty($list) ? array() : $list;
        if(!empty($list)){
            $zidian=base_list();
            foreach($list as $k=>$v){
                $list[$k]['house_type']=$zidian[$v['house_type']] ?? '';
            }
        }
        $this->view->assign('list',$list);
        return $this->view->fetch();
    }
}
